package com.fact.actions;

import java.util.ArrayList;
import java.util.Map;

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.fact.dto.MakeDTO;
import com.fact.dto.UserDTO;
import com.fact.models.MakeService;
import com.fact.utils.AppConstant;

public class Make extends BaseAction implements SessionAware{
	
	private static final long serialVersionUID = 4061156575825032331L;
	private Map<String,Object> session;
	private ArrayList<MakeDTO> makeList;
	
	private String page=AppConstant.MAKE;
	private String pageTitle="FMtool - Make List";

	MakeDTO make=new MakeDTO();
	private String opType;
	MakeService makeService=new MakeService();
	
	public String makeList()
	{
		if(opType==null || (!opType.equalsIgnoreCase("save") && !opType.equalsIgnoreCase("delete") && !opType.equalsIgnoreCase("edit")))
			this.session.put("operation_message", null);
		
		makeList= makeService.getMakeList();
		
		return SUCCESS;
	}
	public String newMakeForm()
	{		
		return SUCCESS;
	}
	public String saveMake()
	{ 
		
		if(makeService.getMake(make.getMakeName())!=null)
			addActionMessage("Make Already Exist.");
		
		if(hasActionMessages())
			return  INPUT;


		if(makeService.saveMake(make)==false)
				{
					addActionMessage("Error in Make Save Operation.");
					return INPUT;
				}
		else
				{
					this.session.put("operation_message", make.getMakeName()+" has successfully been added in Make List.");
					return  SUCCESS;
				}

	}
	
	public String editMake()
	{
		this.session.put("operation_message", null);
		
		MakeDTO makeTmp=makeService.getMake(make.getMakeName());
		boolean editFlag=false;
		if(makeTmp==null)
		{
			editFlag=true;
		}
		else
		{
			if(makeTmp.getMakeId()!=make.getMakeId())
			{
				editFlag=false;
			}
			else
			{
				editFlag=true;
			}
		}
		if(editFlag==true)
		{
			if(makeService.editMake(make)==false)
			{
				addActionMessage("Error in Make Edit Operation.");
				return INPUT;
				
			}
			else
			{
				this.session.put("operation_message","Make with Make Name '"+ make.getMakeName()+"' has successfully been updated.");
				return  SUCCESS;
			}
		}
		else
		{
			addActionMessage("Update Falied. "+make.getMakeName() +" is already in the System.");
			makeList= makeService.getMakeList();
			return INPUT;
		}
	
	}
	public String deleteMake()
	{
		if(makeService.deleteMake(make.getMakeId())==false)
		{
			addActionMessage("Error in Make Delete Operation.");
			return INPUT;
			
		}
		else
		{
			this.session.put("operation_message","Make with Make Name '"+ make.getMakeName()+"' has successfully been deleted from Make List.");
			return  SUCCESS;
		}
	
	}
	
	
	public ArrayList<MakeDTO> getMakeList() {
		return makeList;
	}

	public void setMakeList(ArrayList<MakeDTO> makeList) {
		this.makeList = makeList;
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public String getPageTitle() {
		return pageTitle;
	}

	public void setPageTitle(String pageTitle) {
		this.pageTitle = pageTitle;
	}

	public MakeDTO getMake() {
		return make;
	}

	public void setMake(MakeDTO make) {
		this.make = make;
	}

	public String getOpType() {
		return opType;
	}

	public void setOpType(String opType) {
		this.opType = opType;
	}

	public MakeService getMakeService() {
		return makeService;
	}

	public void setMakeService(MakeService makeService) {
		this.makeService = makeService;
	}

	public void setSession(Map<String, Object> session) 
	{
		this.session = session;
	}
	public ServletContext getServletContext()
	{
		return ServletActionContext.getServletContext();
	}
}
